package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.ManagerUser;
import com.mls.fivedistrict.pojo.RoleMenu;
import com.mls.fivedistrict.pojo.UserRole;
import com.mls.fivedistrict.server.ManagerUserService;
import com.mls.fivedistrict.server.RoleMenuService;
import com.mls.fivedistrict.server.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 角色菜单列表 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-14
 */
@RestController("backendRoleMenuController")
@RequestMapping("/api/manager")
public class RoleMenuController extends BaseController {
    @Autowired
    RoleMenuService roleMenuService;
    @Autowired
    ManagerUserService managerUserService;

    @Autowired
    UserRoleService userRoleService;

    /**
     * 修改角色菜单列表
     *
     * @param roleMenuList
     * @return
     */
    @PutMapping("/v1/roleMenus/{roleId}")
    public ServiceResponse editRoleMenuList(@RequestBody List<RoleMenu> roleMenuList, @PathVariable Long roleId) {
        Optional.ofNullable(roleMenuList).orElseThrow(() -> new CustomerException("角色菜单不能为空"));
        boolean update = roleMenuService.updateBatch(roleMenuList, roleId);
        if (update) {
            return ServiceResponse.createSuccessResponse("配置角色菜单成功");
        } else {
            return ServiceResponse.createErrorResponse("配置角色菜单失败");
        }
    }

    /**
     * 修改单个角色菜单
     *
     * @param roleMenuId
     * @param roleMenu
     * @return
     */
    @PutMapping("/v1/roleMenu/{roleMenuId}")
    public ServiceResponse editRoleMenu(@PathVariable Long roleMenuId, @RequestBody RoleMenu roleMenu) {
        Optional.ofNullable(roleMenuId).orElseThrow(() -> new CustomerException("角色菜单id不能为空"));
        Optional.ofNullable(roleMenu).orElseThrow(() -> new CustomerException("角色菜单不能为空"));
        roleMenu.setId(roleMenuId);
        boolean update = roleMenuService.updateById(roleMenu);
        if (update) {
            return ServiceResponse.createSuccessResponse("修改成功");
        } else {
            return ServiceResponse.createErrorResponse("修改失败");
        }
    }

    /**
     * 删除角色菜单
     *
     * @param roleMenuId
     * @return
     */
    @DeleteMapping("/v1/roleMenu/{roleMenuId}")
    public ServiceResponse deleteRoleMenu(@PathVariable Long roleMenuId) {
        Optional.ofNullable(roleMenuId).orElseThrow(() -> new CustomerException("角色菜单id不能为空"));
        RoleMenu roleMenu = new RoleMenu();
        roleMenu.setId(roleMenuId);
        roleMenu.setDelete(true);
        boolean delete = roleMenuService.updateById(roleMenu);
        if (delete) {
            return ServiceResponse.createSuccessResponse("删除成功");
        } else {
            return ServiceResponse.createErrorResponse("删除失败");
        }
    }

    /**
     * 角色菜单列表
     *
     * @return
     */
    @GetMapping("/v1/roleMenus")
    public ServiceResponse getRoleMenuList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List selectList = roleMenuService.selectList(pageFilter.getWrapper());
        PageInfo<RoleMenu> roleMenuPageInfo = new PageInfo<RoleMenu>(selectList, 5);
        return ServiceResponse.createSuccessResponse(roleMenuPageInfo);
    }

    /**
     * 根据角色id获取角色菜单列表
     *
     * @return
     */
    @GetMapping("/v1/roleMenus/{roleId}")
    public ServiceResponse getRoleMenuList(@PathVariable Long roleId) {
        Optional.ofNullable(roleId).orElseThrow(() -> new CustomerException("角色id不能为空"));
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        Wrapper wrapper = pageFilter.getWrapper();
        if (null == wrapper) {
            wrapper = Condition.create();
        }
        wrapper.eq("role_id", roleId);
        List selectList = roleMenuService.selectList(wrapper);
        PageInfo<RoleMenu> roleMenuPageInfo = new PageInfo<RoleMenu>(selectList, 5);
        return ServiceResponse.createSuccessResponse(roleMenuPageInfo);
    }

    /**
     * 根据管理员id获取角色菜单列表
     *
     * @return
     */
    @GetMapping("/v1/userManagerMenus")
    public ServiceResponse getRoleMenuListByUserManagerId() {
        //登录的管理员用户id
        Long id = getUser().getId();
        //查询改管理员
        Wrapper userRoleWrapper = Condition.wrapper();
        userRoleWrapper.eq("manager_user_id", id);
        List<UserRole> userRoleList = userRoleService.selectList(userRoleWrapper);

        List list = new ArrayList();

        //循环遍历
        for (UserRole userRole : userRoleList) {
            Long roleId = userRole.getRoleId();//角色id
            Wrapper wrapper = Condition.create();
            wrapper.eq("role_id", roleId);
            List selectList = roleMenuService.selectList(wrapper);
            list.addAll(selectList);//将每种角色下的菜单列表放入list
        }
        ManagerUser managerUser = managerUserService.selectById(id);
        return ServiceResponse.createSuccessResponse( managerUser.getIsSupperManager() + "",list);
    }

    /**
     * 单个角色菜单详情
     *
     * @param roleMenuId
     * @return
     */
    @GetMapping("/v1/roleMenu/{roleMenuId}")
    public ServiceResponse getRoleMenu(@PathVariable Long roleMenuId) {
        Optional.ofNullable(roleMenuId).orElseThrow(() -> new CustomerException("角色菜单id不能为空"));
        RoleMenu roleMenu = roleMenuService.selectById(roleMenuId);
        if (null != roleMenu) {
            return ServiceResponse.createSuccessResponse(roleMenu);
        } else {
            return ServiceResponse.createErrorResponse("查无此数据");
        }
    }
}

